/*****************************获取区域树*********************/
    var admin=JSON.parse(localStorage.getItem('userInfo'))
    var getAreaTree={
        url:'org/treeOrgByOrgid',
        fromwho:'ch',
        sendData:{
            orgid :admin.areaid
        }
    }
    getAjax(getAreaTree,
        function(data){
            if(data.result==1){
                var curData=data.data
                setAreaTree(curData)
            }
        },
        function(data){
            console.log(data)
        }
    )
    function  setAreaTree(curData){
        var curMenu = null, zTree_Menu = null;
        var setting = {
            view: {
                showLine: false,
                showIcon: false,
                selectedMulti: false,
                dblClickExpand: false,
            },
            data: {
                key: {
                    children: "nodes",
                    name: "text"
                }
            },
            callback: {
                beforeClick: beforeClick
            }
        };
        var zNodes =curData
        function beforeClick(treeId, treeNode) {
            getOrgUser(treeNode.id)
            if (treeNode.level == 0 ) {
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.expandNode(treeNode);
                return false;
            }
            return true;
        }

        $(document).ready(function(){
            var treeObj = $("#treeDemo");
            $.fn.zTree.init(treeObj, setting, zNodes);
//          zTree_Menu = $.fn.zTree.getZTreeObj("treeDemo");
//          curMenu = zTree_Menu.getNodes()[0].children[0].children[0];
//          zTree_Menu.selectNode(curMenu);
        });

    }
/*****************************获取所有end*********************/
/*****************************获取部门用户*********************/ 
    function getOrgUser(orgid){
        var getUserData={
            url:'user/getUserByOrgid',
            fromwho:'ch',
            sendData:{
                orgid :orgid
            }
        }
        getAjax(getUserData,
            function(data){
                if(data.result==1){
                    var curData=data.data
                    setUserList(curData)
                    if(!$('.mui-collapse').hasClass('mui-active')){
                        $('.mui-collapse').addClass('mui-active')
                    }
                }else{
                    $('form.mui-input-group').find('.proer-div').remove()
                    if($('.mui-collapse').hasClass('mui-active')){
                        $('.mui-collapse').removeClass('mui-active')
                    }
                }
            },
            function(data){
                console.log(data)
            }
        )
        function  setUserList(curData){
            var html=''
            $.each(curData,function(k,v){
                html+=`
                <div class="mui-input-row mui-checkbox mui-left proer-div">
                    <label class="inputcontent clearfix">
                        <div class="img"><img src=${v.headimage ? v.headimage:"img/t.jpg"}></div>
                        <div class="intro">
                            <span>${v.realname ? v.realname:'匿名' }</span>
                            <span>-${v.mtel}</span>
                        </div>
                    </label>
                    <input name="pids" value="${v.id}" type="checkbox">
                </div>
                `
            })
            $('form.mui-input-group').html(html)
        }
    }
/*****************************获取所有end*********************/
/************获对应模版*****************/
    var getOneTempData={
        url:'schedule_manage/sendLetter',
        fromwho:'ld',
        sendData:{
            type:'entrust_pro'
        }
    }
    var letter_id,letter
    getAjax(getOneTempData,
        function(data){
            if(data.result==1){
                var tempData=decodeHtml(data.data.content)
                letter_id=data.data.id
                var reg=/\{\{(.+?)\}\}/g
                var resultArr=[];
                while(result=reg.exec(tempData)){
                    if($.inArray(result[1],resultArr)==-1){
                        resultArr.push(result[1]);
                    }
                }
                console.log(resultArr)
                var html='';
                if(resultArr.length>0){
                    var newArr=changetempname(resultArr)
                    console.log(newArr)
                    html+=`<form class="mui-input-group" id="sendFormTemp">`
                    $.each(newArr,function(k,v){
                        html+=`
                            <div class="mui-input-row">
                                <label>${v}</label>
                            <input type="text" name="${k}" class="mui-input-clear" >
                            </div>
                        `
                    })
                    html+=`</form>`
                }else{
                    html+=`<p style="margin:20px auto;text-align:center">委托函无需输入内容,可直接提交<p>`
                }
                html+=`<button type="button" class="mui-btn mui-btn-primary" id="showtemp" style="margin: 20px auto;display: block;">预览函件</button>`
                $('.mui-content').html(html)
                $(document).on('click','#showtemp',function(){
                    var showjson={}
                    $('#sendFormTemp').find('input').each(function(k,v){
                        var key=$(v).attr('name')
                        var val=$(v).val()
                        showjson[key]=val
                    })
                    var showhtml = template.render(tempData, showjson);
                    mui.alert(showhtml,'预览模版','','','div')
                })
            }else{
                 var html=`<p style="margin:20px auto;text-align:center">暂无委托函,可直接提交<p>`
            }
        },
        function(data){
            console.log(data)
        }
    )
/*****************************获取对应模版end*********************/


/*****************************提交*********************/
    $('#sureproer').on('click',function(){
        var type=$('#protype').val()
        var baseid=GetQueryString('baseid')
        var list=[]
        var ids=[]
        $('form.mui-input-group').find('input[name="pids"]:checked').each(function(k,v){
            ids.push($(v).val())
        })
        if(!type){
            mui.toast('请选择观护类型')
            return false
        }
        if(!ids[0]){
            mui.toast('请选择你要指派的人')
            return false
        }
        $.each(ids,function(k,v){
            list.push({type:type,base_msg_id:baseid,user_id:v})
        })
        var upCaseProData={
            url:'Guard_Record_Manage/appointGuard',
            fromwho:'ld',
            sendData:{
                list:list,
            }
        }
        var waitingDialog
        mui.plusReady(function(){
            waitingDialog = plus.nativeUI.showWaiting("提交中");
        })
        upAjax(upCaseProData,
            function(data){
                mui.plusReady(function(){
                    waitingDialog.close();
                })
                if(data.result==1){
                    mui.toast('观护指派成功')
                    setTimeout(function(){
                        document.location.href = localStorage.getItem('casebackurl')
                    },500)
                }else{
                    mui.toast(data.msg)
                }
            },
            function(data){
                console.log(data)
            }
        )
    })
/*****************************提交end*********************/
